package org.example.com.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.jsonwebtoken.Claims;
import jakarta.annotation.Resource;
import org.example.com.Bean.Book;
import org.example.com.Bean.ShoppingCart;
import org.example.com.config.JwtConfig;
import org.example.com.mapper.ShoppingCartMapper;
import org.example.com.service.Bookservice;
import org.example.com.service.ShoppingCartService;
import org.example.com.util.JsonResult;
import org.example.com.util.RecultTool;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service
public class shoppingServiceimpl extends ServiceImpl<ShoppingCartMapper,ShoppingCart> implements ShoppingCartService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private Bookservice bookservice;

    @Override
    public JsonResult add(String bookId, String token) {
        Claims claims= JwtConfig.parseJWT(token);
        String userId= claims.get("id").toString();
        HashOperations<String,Object,Object> ops=stringRedisTemplate.opsForHash();
        Boolean haskey=ops.hasKey("gwc:"+userId,bookId);
        if(haskey){
            ops.increment("gwc:"+userId,bookId,1);
        }else{
            ops.put("gwc:"+userId,bookId,"1");
        }
        return RecultTool.success("success");
    }
    @Override
    public JsonResult find(String token) {
        Claims claims=JwtConfig.parseJWT(token);
        String userId= claims.get("id").toString();
        HashOperations<String,Object,Object> ops=stringRedisTemplate.opsForHash();
        //当前用户的购物车
        Map<Object,Object> cart=ops.entries("gwc:"+userId);
        List<ShoppingCart> list=new ArrayList<>();
        //循环遍历
        cart.keySet().forEach(bookId->{
            int number=Integer.parseInt(cart.get(bookId).toString());
            ShoppingCart sc=new ShoppingCart();
            sc.setNumber(number);
            Book book=bookservice.getById(bookId.toString());
            sc.setId(book.getId());
            sc.setPublisher(book.getPublisher());
            sc.setBookName(book.getName());
            sc.setPrice(Double.valueOf(book.getPrice()));
            sc.setImageName(book.getImageName());
            list.add(sc);
        });
        return RecultTool.success(list);
    }

    @Override
    public JsonResult changeCartNumber(String token, String bookId, int number) {
        Claims claims=JwtConfig.parseJWT(token);
        String userId= claims.get("id").toString();
        HashOperations<String,Object,Object> ops=stringRedisTemplate.opsForHash();
        ops.increment("gwc:"+userId,bookId,number);
        return RecultTool.success("success");
    }

    @Override
    public JsonResult changeCartNumber2(String token, String bookId, int number) {
        Claims claims=JwtConfig.parseJWT(token);
        String userId= claims.get("id").toString();
        HashOperations<String,Object,Object> ops=stringRedisTemplate.opsForHash();
        ops.put("gwc:"+userId,bookId,number+"");
        return RecultTool.success("success");
    }

    @Override
    public JsonResult removeCartById(String token, String bookId) {
        Claims claims=JwtConfig.parseJWT(token);
        String userId= claims.get("id").toString();
        HashOperations<String,Object,Object> ops=stringRedisTemplate.opsForHash();
        ops.delete("gwc:"+userId,bookId);
        return RecultTool.success("success");
    }

    @Override
    public JsonResult batchRemoveCate(String token, String[] ids) {
        Claims claims=JwtConfig.parseJWT(token);
        String userId= claims.get("id").toString();
        HashOperations<String,Object,Object> ops=stringRedisTemplate.opsForHash();
        for (int i = 0; i < ids.length; i++) {
            ops.delete("gwc:"+userId,ids[i]);
        }
        return RecultTool.success("success");
    }
}
